/**
 * Created by Mr.no.
 * User:yangyun
 * Date:2017/9/7
 * Time:20:38
 */
public class RouteSum {
    /**
     * 返回将一维数组向右旋转K个位置的结果。比如一维数组{1，2,3,4,5}
     * k=2，反转后为{4,5,1,2，3}
     * @param args
     */
    public static void main(String[] args){
        int[] A= new int[]{1, 2, 3, 4, 5};
        int k = 2;
        int[] B = new int[5];
        B=rotateK(A,k);
        for (int b:B
             ) {
            System.out.println(b+" ");
        }

    }
    private static int[] rotateK(int[] A,int k){
        if (A==null || k>=A.length-1){
            return A;
        }
        reverse(A,0,A.length-1);//反转整个数组
        reverse(A,0,k-1);//反转前k个数
        reverse(A,k,A.length-1);//反转剩下的数
        return A;
    }

    private static void reverse(int[] A, int start, int end) {
        while(start<end){
            int temp =A[start];
            A[start] =A[end];
            A[end] = temp;
            start++;
            end--;
        }
    }

}
